/**
 * <p>Copyright:Copyright(c) 2019</p>
 * <p>Company:Professional</p>
 * <p>Package:ces.org.course.event</p>
 * <p>File:FileUploadListener.java</p>
 * <p>类更新历史信息</p>
 *
 * @todo chanlong(陈龙) 创建于 May 17, 2019 4:24:21 PM
 */
package ces.org.course.cuixiaowei.api.event;


import ces.org.course.cuixiaowei.api.entity.DictionaryEntity;
import ces.org.course.cuixiaowei.api.service.DictionaryService;
import ces.org.course.cuixiaowei.cfg.FileProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;
import java.nio.file.Paths;

/**
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date May 17, 2019 4:24:21 PM
 * @version 1.0.2019
 */
@Component
@Slf4j
public class FileUploadListener {

    private static final String UPLOAD_TRIGGER = "event";
    private DictionaryService service;

    @Autowired
    private FileProperties fileProperties;
    // 文件创建时触发
    public void onFileCreate(final File file) {
        service.put(DictionaryEntity.CACHE_KEY, file);
    }

    @EventListener()
    public void listener(final FileUploadEvent event) {
        System.out.println(event.getMessage().getMessage());
        String upLoadPath = event.getMessage().getUploadPath();
        String update = fileProperties.getUpdate();

        if (!StringUtils.hasText(update) && update.equalsIgnoreCase(UPLOAD_TRIGGER) && !StringUtils.hasText(upLoadPath)) {
            log.info("文件简单事件：文件上传路径修改为：[{}]",upLoadPath);
            onFileCreate(Paths.get(event.getMessage().getUploadPath()).toFile());
        }

    }



}
